package com.ctvit.cch.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctvit.cch.core.mapper.HotCategoryFavoriteMapper;
import com.ctvit.cch.core.pojo.domain.HotCategoryFavorite;
import com.ctvit.cch.core.pojo.dto.hot.HotCategoryFavoriteAddDTO;
import com.ctvit.cch.core.pojo.dto.hot.HotCategoryFavoriteBatchAddDTO;
import com.ctvit.cch.core.service.IHotCategoryFavoriteService;
import com.ruoyi.common.utils.SecurityUtils;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 热点分类收藏Service业务层处理
 *
 * @author heyingcheng
 * @date 2025/11/07
 */
@Service
public class HotCategoryFavoriteServiceImpl extends ServiceImpl<HotCategoryFavoriteMapper, HotCategoryFavorite>
        implements IHotCategoryFavoriteService {

    @Override
    public boolean add(HotCategoryFavoriteAddDTO dto) {
        HotCategoryFavorite entity = new HotCategoryFavorite();
        entity.setCategoryId(dto.getCategoryId());
        entity.setUserId(SecurityUtils.getUserId());

        return save(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean batchAdd(HotCategoryFavoriteBatchAddDTO dto) {
        Long userId = SecurityUtils.getUserId();

        remove(new LambdaQueryWrapper<HotCategoryFavorite>().eq(HotCategoryFavorite::getUserId, userId));

        List<HotCategoryFavorite> entities = dto.getCategoryIds().stream().map(categoryId -> {
            HotCategoryFavorite entity = new HotCategoryFavorite();
            entity.setCategoryId(categoryId);
            entity.setUserId(userId);
            return entity;
        }).collect(Collectors.toList());

        return saveBatch(entities);
    }

}